<template>
  <div class="login">
    <div class="login_box">
      <div class="img_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <div class="login_from">
        <el-form
          :model="loginForm"
          :rules="loginFormRules"
          ref="ruleForm"
          label-width="100px"
        >
          <el-form-item label="用户名" prop="username">
            <el-input v-model="loginForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="loginForm.password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="login">登录</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import loginAPI from "../http/API/Login"
export default {
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456",
      },
      loginFormRules: {
        name: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 10,
            message: "长度在 6 到 10 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    login() {
      this.$refs.ruleForm.validate(async (v) => {
        if (v) {
          let res = await loginAPI.login(this.loginForm);
          console.log(res);
          if (res.data.meta.status == 200) {
            this.$message.success(res.data.meta.msg);
            localStorage.setItem("token", res.data.data.token);
            this.$router.push("/home");
          } else {
            this.$message.error(res.data.meta.msg);
          }
        }
      });
    },
    resetForm() {
      this.$refs.ruleForm.resetFields();
    },
  },
};
</script>

<style scoped  >
.login {
  height: 100%;
  background: #2b5b6b;
  position: relative;
}

.login_box {
  width: 450px;
  height: 300px;
  border-radius: 3px;
  background: #fff;
  position: absolute;
  left: 50%;
  top: 50%;
  margin-left: -225px;
  margin-top: -150px;
}
.img_box {
  width: 130px;
  height: 130px;
  position: absolute;
  left: 50%;
  transform: translate(-50%);
  border: 1px solid #ccc;
  background: #fff;
  top: -75px;
  padding: 10px;
  border-radius: 50%;
}
.img_box {
  width: 130px;
  height: 130px;
  position: absolute;
  left: 50%;
  transform: translate(-50%);
  border: 1px solid #ccc;
  background: #fff;
  top: -75px;
  padding: 10px;
  border-radius: 50%;
}
img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: #efefef;
}
.login_from {
  width: 100%;
  height: 100%;
  margin-left: -25px;
  margin-top: 80px;
}
</style>>

